package com.foreknow.elm.service.Impl;

import com.foreknow.elm.dao.Impl.UserDaoImpl;
import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    @Override
    public User getUserByIdByPass(String userId, String password) {
        User user=new User();
        UserDao userDao=new UserDaoImpl();
        try {
            user=userDao.getUserByIdByPass(userId,password);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return user;
    }

    @Override
    public int getUserById(String userId) {
        int i=0;
        UserDao userDao=new UserDaoImpl();
        try {
            i=userDao.getUserById(userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int saveUser(String userId, String password, String userName, Integer userSex) {
        int i=0;
        UserDao userDao=new UserDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            i=userDao.saveUser(userId,password,userName,userSex);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return i;
    }

    public static void main(String[] args) {
        UserService us=new UserServiceImpl();

        int i= us.saveUser("12345","132","TOM",1);
        System.out.println(i);

//        int i=us.getUserById("12345671111");
//        System.out.println(i);

//        User user= us.getUserByIdByPass("12345671111","123");
//        System.out.println(user.getUserName());
    }
}
